// let nodeData = {}
// nodeData.isViewNode = true;
// nodeData.isShow = true;
// nodeData.name = "root";
// nodeData.nodeDepth = 0;
// nodeData.index = 0;
// nodeData.childDic = {}
// nodeData.parent = null;


let JSTree = function () {
}
JSTree.prototype = {
    nodeData: null,
    parentDiv: null,
    onClick: null,
    startPosX: 10,
    startPosY: 100,
    treeHigh: 600,
    sliderLen: 100,
    temp_parentNodeObj: null,
    temp_currShowNodeIndex: 0,
    o_nodeObjArr: [],
    temp_showNodeDataArr: [],
    Init: function (nodeData, m_parentDiv, onClick) {
        this.nodeData = nodeData;
        this.m_parentDiv = m_parentDiv;
        this.onClick = onClick;
        this.Init_CreateSlider(m_parentDiv);

        let number = this.treeHigh / 15;
        for (let i = 0; i < number; i++) {
            let node = this.Init_createNode(m_parentDiv, this);
            this.o_nodeObjArr[this.o_nodeObjArr.length] = node;
        }
    },
    Init_CreateSlider: function (m_parentDiv) {
        let wide = 10;
        let high = this.treeHigh;
        let style_position = "absolute";//static,absolute,fixed,relative,inherit
        let m_sliderBg = document.createElement('a');
        m_sliderBg.style.position = style_position;//绝对位置-非常重要的属性 
        m_sliderBg.style.left = this.startPosX + "px";
        m_sliderBg.style.top = this.startPosY + "px";
        m_sliderBg.style.width = wide + "px";
        m_sliderBg.style.height = high + "px";
        m_sliderBg.style.zIndex = 0;
        m_sliderBg.style.backgroundColor = "#110033";
        m_parentDiv.appendChild(m_sliderBg);


        let m_slider = document.createElement('a');
        m_slider.style.position = style_position;//绝对位置-非常重要的属性 
        m_slider.style.left = this.startPosX + "px";
        m_slider.style.top = this.startPosY + "px";
        m_slider.style.width = wide + "px";
        m_slider.style.height = this.sliderLen + "px";
        m_slider.style.zIndex = 0;
        m_slider.style.backgroundColor = "#ff0099";
        m_parentDiv.appendChild(m_slider);
        m_slider.o_thisObj = this;
        m_slider.o_posXIsFixed = true;
        m_slider.o_posYmin = this.startPosY;
        m_slider.o_posYMax = this.startPosY + high - this.sliderLen;
        m_slider.d_dragEvent = this.m_sliderDrag;
        CardDrag_SetDrag(m_slider);
    },
    Init_createNode: function (m_parentDiv, treeObj) {
        //console.log("创建a......." + keyName)
        let wide = 200;
        let high = 15;
        let style_position = "absolute";//static,absolute,fixed,relative,inherit
        let o_nodeObj = {};
        // o_nodeObj.keyName = tree_parameter.temp_nodeData.name;
        // o_nodeObj.m_parentNodeObj = tree_parameter.temp_parentNodeObj;
        // o_nodeObj.nodeData = tree_parameter.temp_nodeData;
        // o_nodeObj.onClick = tree_parameter.onClick;
        o_nodeObj.treeObj = treeObj;
        o_nodeObj.posX = 0;
        o_nodeObj.posY = 0;
        o_nodeObj.nodeCount = 0;


        let parentLine = document.createElement('a');
        o_nodeObj.m_parentLine = parentLine;
        parentLine.m_ode = o_nodeObj;
        parentLine.style.position = style_position;//绝对位置-非常重要的属性 
        parentLine.style.width = high + "px";
        parentLine.style.height = "1px";
        parentLine.style.zIndex = 0;
        parentLine.style.backgroundColor = "#776677";
        m_parentDiv.appendChild(parentLine);

        let subLine = document.createElement('a');
        o_nodeObj.m_subLine = subLine;
        subLine.o_nodeObj = o_nodeObj;
        subLine.style.position = style_position;//绝对位置-非常重要的属性 
        subLine.style.width = "1px";
        subLine.style.height = "1px";
        subLine.style.zIndex = 0;
        subLine.style.backgroundColor = "#776677";
        m_parentDiv.appendChild(subLine);


        let img = document.createElement('a');
        o_nodeObj.m_img = img;
        img.o_nodeObj = o_nodeObj;
        img.style.position = style_position;//绝对位置-非常重要的属性 
        img.style.width = high + "px";
        img.style.height = high + "px";
        img.style.zIndex = 0;
        img.style.backgroundColor = "#ffaa00";
        m_parentDiv.appendChild(img);

        let text = document.createElement('a');
        o_nodeObj.m_text = text;
        text.o_nodeObj = o_nodeObj;
        //text.innerText = tree_parameter.temp_nodeData.name;
        text.style.position = style_position;//绝对位置-非常重要的属性 
        text.style.width = wide + "px";
        text.style.height = high + "px";
        text.style.zIndex = 0;
        text.style.fontSize = "5px";
        text.style.backgroundColor = "#aaff99";
        m_parentDiv.appendChild(text);


        o_nodeObj.S_SetIsShow = function (isShow) {
            if (isShow) {
                this.m_parentLine.style.display = "block";
                this.m_subLine.style.display = "block";
                this.m_img.style.display = "block";
                this.m_text.style.display = "block";
            } else {
                this.m_parentLine.style.display = "none";
                this.m_subLine.style.display = "none";
                this.m_img.style.display = "none";
                this.m_text.style.display = "none";
            }
            if (this.m_childNodeArr != null && this.m_childNodeArr.length > 0) {
                for (let i3 = 0; i3 < this.m_childNodeArr.length; i3++) {
                    this.m_childNodeArr[i3].S_SetIsShow(isShow);
                }
            }
        }


        o_nodeObj.S_RefreshDataShow = function (nodeData, depth, nodeCount) {
            this.posX = depth * 20;
            this.posY = nodeCount * (high + 2);
            this.o_nodeData = nodeData;
            let offecttX = 0;
            let startPosX = this.treeObj.startPosX;
            let startPosY = this.treeObj.startPosY;
            this.m_parentLine.style.left = (startPosX + this.posX + offecttX) + "px";
            this.m_parentLine.style.top = (startPosY + this.posY + high * 0.5) + "px";
            offecttX += this.m_parentLine.offsetWidth;

            this.m_img.style.left = (startPosX + this.posX + offecttX) + "px";
            this.m_img.style.top = (startPosY + this.posY) + "px";
            offecttX += this.m_img.offsetWidth + 5;

            this.m_text.style.left = (startPosX + this.posX + offecttX) + "px";
            this.m_text.style.top = (startPosY + this.posY) + "px";
            this.m_text.innerText = nodeData.name;
            //refreshParameter.temp_tagStr += "___."
            //cc.log(refreshParameter.temp_tagStr + this.keyName + "...." + this.posX + "...." + this.m_text.style.top + "....." + this.nodeCount);
            if (nodeData.childKeyArr == null || nodeData.childKeyArr.length == 0) {
                this.m_img.style.backgroundColor = "#cccccc";
            } else {
                if (nodeData.isShow) {
                    this.m_img.style.backgroundColor = "#ffaa00";
                } else {
                    this.m_img.style.backgroundColor = "#ff33ff";
                }
            }

            let lastSubNode = null;
            if (this.m_childNodeArr != null && this.m_childNodeArr.length > 0) {
                let tagStr = refreshParameter.temp_tagStr;
                for (let i3 = 0; i3 < this.m_childNodeArr.length; i3++) {
                    let childNode = this.m_childNodeArr[i3];
                    // if (parentNodeObj.nodeCount < refreshParameter.maxShowNumber) {
                    childNode.S_SetIsShow(nodeData.isShow);
                    if (nodeData.isShow) {
                        refreshParameter.temp_parentNodeObj = this;
                        refreshParameter.temp_tagStr = tagStr;
                        childNode.S_Refresh(refreshParameter);
                        parentNodeObj.nodeCount = childNode.nodeCount;
                        lastSubNode = childNode;
                    }
                    // } else {
                    //     childNode.S_SetIsShow(false);
                    // }
                }
            }

            return this;
        }
        o_nodeObj.S_RefreshSubLineShow = function (isHasSubData, lastSubObj_posX, lastSubObj_posY) {
            let startPosX = this.treeObj.startPosX;
            let startPosY = this.treeObj.startPosY;
            if (isHasSubData == null) {
                this.m_subLine.style.display = "none";
            } else {
                this.m_subLine.style.display = "block";
                this.m_subLine.style.width = "1px";
                this.m_subLine.style.height = (lastSubObj_posY - this.posY) + "px";
                this.m_subLine.style.left = (startPosX + lastSubObj_posX) + "px";
                this.m_subLine.style.top = (startPosY + this.posY + high * 0.5) + "px";
            }
        }
        let S_onClick = function () {
            let nodeObj = this.o_nodeObj;
            let o_nodeData = nodeObj.o_nodeData;
            o_nodeData.isShow = !o_nodeData.isShow;
            //console.log("ddddd.............." + nodeObj.keyName + "......" + nodeObj.isShow)
            let parentNodeObj = nodeObj;
            while (parentNodeObj.m_parentNodeObj != null) {
                parentNodeObj = parentNodeObj.m_parentNodeObj;
                //console.log("ddddd..............." + parentNodeObj.keyName)
            }
            if (parentNodeObj.initOffectX == null) {
                parentNodeObj.initOffectX = 0;
            }
            if (parentNodeObj.initOffectY == null) {
                parentNodeObj.initOffectY = 0;
            }
            let refreshParameter = {}
            refreshParameter.temp_parentNodeObj = null;
            refreshParameter.startPosX = 0;
            refreshParameter.startPosY = 0;
            refreshParameter.temp_tagStr = "";
            refreshParameter.maxShowNumber = 40;
            parentNodeObj.S_Refresh(refreshParameter);
        }
        o_nodeObj.m_img.onclick = S_onClick;
        o_nodeObj.m_text.onclick = function () {
            if (o_nodeObj.onClick != null) {
                o_nodeObj.onClick(o_nodeObj.nodeData);
            }
        }

        return o_nodeObj;
    },

    m_sliderDrag: function (thisObj, type, press_posX, press_posY) {
        if (type == 3) {
            let len = press_posY;// - thisObj.startPosY
            let ratio = len / (thisObj.treeHigh - thisObj.sliderLen);

            thisObj.temp_showNodeDataArr = [];
            thisObj._ResetShowNodeObjArr(thisObj.nodeData);

            let numberf = ratio * thisObj.temp_showNodeDataArr.length;
            let number = Math.round(numberf)

            let nodeData = thisObj.temp_showNodeDataArr[number];

            thisObj.temp_currShowNodeIndex = 0;
            //thisObj.refresh_ShowData(thisObj.nodeData);
            thisObj.refresh_ShowDataArr(nodeData);
        }
    },
    _ResetShowNodeObjArr: function (_nodeData) {
        if (_nodeData.isShow) {
            let thisObj = this;
            let len = this.temp_showNodeDataArr.length;
            this.temp_showNodeDataArr[len] = _nodeData;
            if (_nodeData.childDic != null) {
                Object.keys(_nodeData.childDic).forEach(function (key) {
                    thisObj._ResetShowNodeObjArr(_nodeData.childDic[key]);
                });
            }
        }
    },
    refresh_ShowDataArr: function (nodeData) {
        let _ShowDataArr = function (thisObj, childDic, index) {
            let childKeyArr = Object.keys(childDic);
            for (let i = index; i < childKeyArr.length; i++) {
                thisObj.refresh_ShowData(nodeData);
            }
        }
        this.refresh_ShowData(nodeData);
        if (nodeData.parent != null) {
            _ShowDataArr(this, nodeData.parent.childDic, nodeData.index)
        }
    },
    refresh_ShowData: function (nodeData) {
        let thisObj = this;
        if (this.temp_currShowNodeIndex < this.o_nodeObjArr.length) {
            thisObj.temp_lastSubObj = null;
            let nodeObj = this.o_nodeObjArr[this.temp_currShowNodeIndex]
            let childDic = nodeData.childDic;
            nodeData.childKeyArr = Object.keys(childDic)
            nodeObj.S_RefreshDataShow(nodeData, nodeData.nodeDepth, this.temp_currShowNodeIndex);
            this.temp_currShowNodeIndex++;

            nodeData.childKeyArr.forEach(function (key) {
                let _nodeData = childDic[key]
                if (_nodeData.isViewNode) {
                    thisObj.temp_lastSubObj = thisObj.refresh_ShowData(_nodeData);
                }
            });
            if (thisObj.temp_lastSubObj == null) {
                let posY = this.startPosY + this.treeHigh;
                if (nodeData.childKeyArr != null && nodeData.childKeyArr.length > 0) {
                    nodeObj.S_RefreshSubLineShow(true, 0, posY);
                } else {
                    nodeObj.S_RefreshSubLineShow(false, 0, 0);
                }
            } else {
                nodeObj.S_RefreshSubLineShow(true, thisObj.temp_lastSubObj.posX, thisObj.temp_lastSubObj.posY);
            }
            return nodeObj;
        } else {
        }
        return null;
    },
    GetNodeObj: function (index, nodeDepth) {

    }
}


JSTree.Tree_ShowWeType = function (m_rootDiv) {
    let nodeData = {}
    nodeData.name = "WeData数据类型";
    nodeData.isViewNode = true;
    nodeData.isShow = true;
    nodeData.nodeDepth = 0;
    nodeData.index = 0;
    nodeData.childDic = {}
    for (let onlyPathTag in WeDataDic) {
        //cc.log("加载..........", onlyPathTag)
        let weObj = WeDataDic[onlyPathTag];
        let arr = onlyPathTag.split('_');
        let parentNode = nodeData;
        for (let i in arr) {
            let strId = arr[i];
            let subNodeData = parentNode.childDic[strId];
            if (subNodeData == null) {
                subNodeData = {}
                subNodeData.name = strId;
                subNodeData.nodeDepth = i;
                subNodeData.index = Object.keys(parentNode.childDic).length;
                subNodeData.isViewNode = true;
                subNodeData.isShow = true;
                subNodeData.childDic = {}
                subNodeData.parent = parentNode;
                parentNode.childDic[strId] = subNodeData;
            }
            parentNode = subNodeData;
        }
        parentNode.isViewNode = true;
        parentNode.isShow = true;
        parentNode.name = onlyPathTag + "_" + weObj.m_className;
        parentNode.data = weObj;
    }
    let jsTree = new JSTree();
    jsTree.Init(nodeData, m_rootDiv, function (nodeData) {
        if (nodeData.data != null) {
            // let thisObj = uiRoot.o_ArtTreeView
            // let fullPath = thisObj.prjDir + "/" + nodeData.data.path
            // cc.log("加载对象........" + fullPath)
            // WebPost_FileRead(fullPath, "text", thisObj.S_OpenWeDataView)
        }
    });

    jsTree.initOffectX = 0;
    jsTree.initOffectY = 60;
}